#include <sdk.h>
#include "editkeywordsdlg.h"

BEGIN_EVENT_TABLE( EditKeywordsDlg, wxScrollingDialog )
  EVT_SPINCTRL( wxID_ANY, EditKeywordsDlg::OnSetChange )
END_EVENT_TABLE()

EditKeywordsDlg::EditKeywordsDlg( wxWindow* parent, EditorColourSet* theme, HighlightLanguage lang, const wxArrayString& descr ) : m_pTheme( theme ), m_Lang( lang ), descriptions( descr ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置关键字.xrc" );
  if( !xml_res->Load( resPath ) )  {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( F( _T( "文件丢失: %s " ), resPath.wx_str() ) );
    return;
  }
  if( !xml_res->LoadObject( this, parent, _T( "dlgEditLangKeywords" ), _T( "wxScrollingDialog" ) ) ) {
    xml_File_lock = false;
    Manager::Get()->GetLogManager()->LogInf( _T( "资源丢失" ) );
    return;
  }
  xml_File_lock = true;
  XRCCTRL( *this, "wxID_OK", wxButton )->SetDefault();
  spnSet = XRCCTRL( *this, "spnSet", wxSpinCtrl );
  txtKeywords = XRCCTRL( *this, "txtKeywords", wxTextCtrl );
  UpdateDlg();
}

EditKeywordsDlg::~EditKeywordsDlg() {
}

void EditKeywordsDlg::OnSetChange( M_Code_unused wxSpinEvent& event ) {
  m_pTheme->SetKeywords( m_Lang, m_LastSet, txtKeywords->GetValue() );
  UpdateDlg();
}

void EditKeywordsDlg::UpdateDlg() {
  m_LastSet = spnSet->GetValue() - 1;
  txtKeywords->SetValue( m_pTheme->GetKeywords( m_Lang, m_LastSet ) );
  wxStaticText* txtDescription = XRCCTRL( *this, "txtDescription", wxStaticText );
  if( descriptions.GetCount() > ( size_t )m_LastSet ) {
    txtDescription->SetLabel( descriptions[m_LastSet] + _T( ":" ) );
  } else {
    txtDescription->SetLabel( _T( "Keywords:" ) );
  }
}
